package cn.hxt.system.controller;


import cn.hxt.basic.util.AjaxResult;
import cn.hxt.basic.util.PageList;
import cn.hxt.system.domain.Type;
import cn.hxt.system.query.TypeQuery;
import cn.hxt.system.service.ITypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/detail")
/*@GetMapping
@PostMapping
@DeleteMapping
@PutMapping
@PatchMapping*/
@Api(tags = "类型接口",description = "类型接口详细描述")
public class TypeController {

   /* @GetMapping("/{id}")
    public Employee getById(@PathVariable("id") Long id){
        return null;
    }*/

    @Autowired
    private ITypeService typeService;

    /**
     * 写接口三要数：
     * 1请求地址是什么
     *
     * @return
     */


    //新增and修改
    @PostMapping("/addOnUpdate")
    @ApiOperation(value = "类型修改及新增",notes = "如果有id就是修改，没有就是新增")
    public AjaxResult addOnUpdate(@RequestBody Type type){
        try {
            //判断是否携带id
            if(type.getId() ==null){
                //没id就新增
                typeService.add(type);
            }else{
                //有就是修改
                typeService.update(type);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            //打印提示
            return AjaxResult.me().setSuccess(false).setMessage("业务繁忙，请稍后！！！");
        }

    }



    //删除一条数据
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            typeService.delete(id);
            //成功
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            //使用链式
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }


    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            typeService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    //分页
    @PostMapping("/page")
    public PageList<Type> query(@RequestBody TypeQuery query){
        return typeService.queryPage(query);
    }


    //获取全部数据
    @GetMapping("/list")
    public List<Type> findAll(){
        return typeService.queryAll();
    }


    //获取一条数据    GET
    @GetMapping("/{id}")
    public Type findById(@PathVariable("id") Long id){
        return  typeService.queryById(id);
    }


}
